@echo off
chcp 65001 >nul
setlocal enabledelayedexpansion

REM =====================================================
REM 智能协同云图库 - Windows快速启动脚本
REM 创建时间: 2024-01-15
REM 版本: 2.0
REM 描述: 一键部署和启动智能协同云图库系统 (Windows版)
REM =====================================================

set PROJECT_NAME=智能协同云图库
set PROJECT_VERSION=2.0
set PROJECT_DIR=%~dp0..

REM 颜色定义 (Windows CMD)
set RED=[91m
set GREEN=[92m
set YELLOW=[93m
set BLUE=[94m
set PURPLE=[95m
set CYAN=[96m
set NC=[0m

echo.
echo %CYAN%=======================================================
echo 🎨 %PROJECT_NAME% v%PROJECT_VERSION%
echo 🚀 革命性的多模态AI图片处理平台
echo =======================================================%NC%
echo.

REM 检查Java环境
echo %BLUE%[INFO] 检查Java环境...%NC%
java -version >nul 2>&1
if errorlevel 1 (
    echo %RED%[ERROR] Java未安装，请安装JDK 17或更高版本%NC%
    pause
    exit /b 1
)

for /f "tokens=3" %%i in ('java -version 2^>^&1 ^| findstr /i "version"') do (
    set JAVA_VERSION=%%i
    set JAVA_VERSION=!JAVA_VERSION:"=!
)
echo %GREEN%[SUCCESS] Java版本检查通过: !JAVA_VERSION!%NC%

REM 检查Maven环境
echo %BLUE%[INFO] 检查Maven环境...%NC%
mvn -version >nul 2>&1
if errorlevel 1 (
    echo %RED%[ERROR] Maven未安装，请安装Maven 3.6或更高版本%NC%
    pause
    exit /b 1
)
echo %GREEN%[SUCCESS] Maven检查通过%NC%

REM 检查端口占用
echo %BLUE%[INFO] 检查端口8080占用情况...%NC%
netstat -an | findstr :8080 >nul 2>&1
if not errorlevel 1 (
    echo %YELLOW%[WARNING] 端口8080已被占用，请释放该端口%NC%
    pause
)

REM 进入项目目录
cd /d "%PROJECT_DIR%"

REM 创建配置文件
echo %BLUE%[INFO] 检查配置文件...%NC%
if not exist "src\main\resources\application.yml" (
    echo %BLUE%[INFO] 创建默认配置文件...%NC%
    (
        echo server:
        echo   port: 8080
        echo.
        echo spring:
        echo   datasource:
        echo     url: jdbc:mysql://localhost:3306/yu_picture?useUnicode=true^&characterEncoding=utf-8^&useSSL=false^&serverTimezone=Asia/Shanghai
        echo     username: root
        echo     password: 123456
        echo     driver-class-name: com.mysql.cj.jdbc.Driver
        echo.
        echo   redis:
        echo     host: localhost
        echo     port: 6379
        echo     database: 0
        echo.
        echo   jpa:
        echo     hibernate:
        echo       ddl-auto: update
        echo     show-sql: false
        echo.
        echo logging:
        echo   level:
        echo     com.yupi.yupicturebackend: INFO
        echo   file:
        echo     name: logs/yu-picture.log
    ) > "src\main\resources\application.yml"
    echo %GREEN%[SUCCESS] 配置文件创建完成%NC%
) else (
    echo %GREEN%[SUCCESS] 配置文件已存在%NC%
)

REM 数据库初始化提示
echo.
echo %YELLOW%[INFO] 数据库初始化说明:%NC%
echo   1. 请确保MySQL服务已启动
echo   2. 请手动执行以下命令初始化数据库:
echo      mysql -u root -p ^< sql\init_database.sql
echo   3. 或者使用MySQL客户端工具导入 sql\init_database.sql
echo.
set /p continue="是否已完成数据库初始化？(y/n): "
if /i not "%continue%"=="y" (
    echo %YELLOW%[INFO] 请先完成数据库初始化，然后重新运行此脚本%NC%
    pause
    exit /b 0
)

REM 构建项目
echo %BLUE%[INFO] 清理项目...%NC%
call mvn clean -q
if errorlevel 1 (
    echo %RED%[ERROR] 项目清理失败%NC%
    pause
    exit /b 1
)

echo %BLUE%[INFO] 编译项目...%NC%
call mvn compile -q
if errorlevel 1 (
    echo %RED%[ERROR] 项目编译失败%NC%
    pause
    exit /b 1
)
echo %GREEN%[SUCCESS] 项目编译成功%NC%

echo %BLUE%[INFO] 运行测试...%NC%
call mvn test -q
if errorlevel 1 (
    echo %YELLOW%[WARNING] 部分测试失败，但继续构建%NC%
) else (
    echo %GREEN%[SUCCESS] 测试通过%NC%
)

echo %BLUE%[INFO] 打包项目...%NC%
call mvn package -DskipTests -q
if errorlevel 1 (
    echo %RED%[ERROR] 项目打包失败%NC%
    pause
    exit /b 1
)
echo %GREEN%[SUCCESS] 项目打包成功%NC%

REM 创建日志目录
if not exist "logs" mkdir logs

REM 启动应用
echo %BLUE%[INFO] 启动智能协同云图库...%NC%

REM 查找JAR文件
for %%f in (target\yu-picture-backend-*.jar) do set JAR_FILE=%%f

if not defined JAR_FILE (
    echo %RED%[ERROR] 找不到JAR文件%NC%
    pause
    exit /b 1
)

REM 启动应用
start "Yu Picture Backend" java -jar "%JAR_FILE%" --spring.profiles.active=prod

REM 等待应用启动
echo %BLUE%[INFO] 等待应用启动...%NC%
timeout /t 10 /nobreak >nul

REM 检查应用是否启动成功
for /l %%i in (1,1,30) do (
    curl -s http://localhost:8080/actuator/health >nul 2>&1
    if not errorlevel 1 (
        echo %GREEN%[SUCCESS] 应用启动成功！%NC%
        goto :startup_success
    )
    echo %BLUE%[INFO] 等待应用启动... (%%i/30)%NC%
    timeout /t 2 /nobreak >nul
)

echo %RED%[ERROR] 应用启动超时，请检查日志%NC%
pause
exit /b 1

:startup_success
echo.
echo %GREEN%=======================================================
echo 🎉 智能协同云图库启动成功！
echo.
echo 📊 系统信息:
echo    • 应用地址: http://localhost:8080
echo    • API文档: http://localhost:8080/swagger-ui.html
echo    • 健康检查: http://localhost:8080/actuator/health
echo    • 日志文件: %PROJECT_DIR%\logs\
echo.
echo 🚀 核心功能:
echo    • AI图片生成: POST /api/ai/generation/create
echo    • NFT铸造: POST /api/nft/mint
echo    • 智能搜索: GET /api/search/intelligent
echo    • 游戏创建: POST /api/game/puzzle/create
echo    • 协作项目: POST /api/collaboration/project/create
echo.
echo 🔧 管理命令:
echo    • 查看日志: type logs\yu-picture.log
echo    • 停止应用: 关闭Java应用窗口
echo =======================================================%NC%
echo.

REM 创建停止脚本
(
    echo @echo off
    echo echo 正在停止智能协同云图库...
    echo taskkill /f /im java.exe /fi "WINDOWTITLE eq Yu Picture Backend"
    echo echo 应用已停止
    echo pause
) > scripts\stop.bat

echo %GREEN%[INFO] 停止脚本已创建: scripts\stop.bat%NC%
echo %BLUE%[INFO] 按任意键打开浏览器访问应用...%NC%
pause >nul

REM 打开浏览器
start http://localhost:8080

echo %GREEN%[SUCCESS] 智能协同云图库已成功启动并运行！%NC%
pause
